package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.mapper.PurchaseGoodsMapper;
import org.jeecg.modules.mapper.PurchaseMapper;
import org.jeecg.modules.pojo.Purchase;
import org.jeecg.modules.pojo.PurchaseGoods;
import org.jeecg.modules.service.PurchaseGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: Andy.Li
 * @Date: 2022.04.27 17:34:48
 */
@Service
public class PurchaseGoodsServiceImpl implements PurchaseGoodsService {
    @Resource
    private PurchaseGoodsMapper purchaseGoodsMapper;
    @Resource
    private PurchaseMapper purchaseMapper;

    @Override
    public List<PurchaseGoods> getGoods(Integer purchaseListId) {
        QueryWrapper<PurchaseGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_list_id", purchaseListId);

        return purchaseGoodsMapper.selectList(wrapper);
    }

    @Transactional(readOnly = true)
    @Override
    public List<PurchaseGoods> getPurchaseInfoByPurNum(Purchase purchase) {
        if (purchase == null) {
            return null;
        }
        Integer purchaseListId = null;
        //先查询purchaselist的id
        QueryWrapper<Purchase> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_number", purchase.getPurchaseNumber());
        List<Purchase> purchases = purchaseMapper.selectList(wrapper);
        if (purchases.size() == 1) {
            purchaseListId = purchases.get(0).getPurchaseListId();
        }
        //根据id查询purchaseListGoods的列表
        QueryWrapper<PurchaseGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_list_id", purchaseListId);
        return purchaseGoodsMapper.selectList(queryWrapper);
    }
}
